package com.lolan.training;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.math.BigInteger;
import java.net.URI;
import java.security.InvalidKeyException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.*;

import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;

public class HttpSender {

    public static JSONObject postResponse(String url, JSONObject jsonParam) {

	HttpClient client = HttpClients.createDefault();
	JSONObject jsonObject = null;
	HttpPost post = new HttpPost(url);
	try {
	    StringEntity s = new StringEntity(jsonParam.toString(), "UTF-8");
	    s.setContentEncoding("UTF-8");
	    s.setContentType("application/json");
	    post.setHeader("X-ODOO-DATABASE-NAME", "tgtest4");
	    post.setEntity(s);
	    // post.setHeader("Content-Type","application/json");
	    HttpResponse res = client.execute(post);
	    if (res != null) {
		jsonObject = JSONObject.parseObject(EntityUtils.toString(res.getEntity()));
		if (res.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
		    /* 返回json格式 */
		    // jsonObject = JSONObject.parseObject(EntityUtils.toString(res.getEntity()));
		}
	    }
	} catch (IOException e) {
	    System.out.println("接口调用出错！");
	    e.printStackTrace();
	}
	return jsonObject;
    }

    public static String getMAC(String apiRoute, String secretKey, long timestamp, String jsonStr, String method)
	    throws NoSuchAlgorithmException, InvalidKeyException, UnsupportedEncodingException {
	System.out.println("json is " + jsonStr);
	String secret = String.format("ts@%s|route@%s|sec@%s|method@%s", timestamp, apiRoute, secretKey, method);
	System.out.println("secret is " + secret);
	byte[] secBytes = secret.getBytes("UTF-8");
	MessageDigest mdSHA512 = MessageDigest.getInstance("SHA-512");
	mdSHA512.update(secBytes);
	byte[] macKey = mdSHA512.digest();
	System.out.println("key is " + new BigInteger(1, macKey).toString(16));
	byte[] bodyBytes = jsonStr.getBytes("utf-8");
	SecretKeySpec macKeySpec = new SecretKeySpec(macKey, "HmacSHA256");
	Mac sha256_HMAC = Mac.getInstance("HmacSHA256");
	sha256_HMAC.init(macKeySpec);
	byte[] hash = sha256_HMAC.doFinal(bodyBytes);
	String sign = new BigInteger(1, hash).toString(16);
	System.out.println("sign is " + sign);
		return sign;
    }

    public static void main(String[] args)
	    throws NoSuchAlgorithmException, UnsupportedEncodingException, InvalidKeyException {
	// String jsonString =
	// "{\"params\":{\"client_id\":\"ahUAaKExrLkbprJhUFQKlqKioEadXVYr\",\"sign\": \"16ae8879cb1b4d7d48c3ea293495d8c8\",\"param\":{\"subject\": \"value1\",\"content\": \"value2\",\"parameters\": {\"receivers\": [2]}},\"timestamp\": 2683171883}}";

	// System.out.println(timestamp);
	// String jsonString = "{\"timestamp\":"+timestamp+"}"; // 请求体内容
//	long timestamp = System.currentTimeMillis() / 1000; // 十位时间戳
	long timestamp = 1698908263L;
	String jsonString = "{\"write_date_to\":\"2023-12-31\",\"write_date_from\":\"2023-01-01\"}"; // 请求体内容
	String secretKey = "ERi0QYTa7pULOm0JfWTAT9m2KLPPreSt"; // 盐值
	String apiRoute = "/studio/api_special/tonggang/sys_admin_orgs/search_data"; // 请求的API路径
	String sign = getMAC(apiRoute, secretKey, timestamp, jsonString, "post");
	String jparam = "{\"params\":{\"timestamp\":" + timestamp
		+ ",\"client_id\":\"ahUAaKExrLkbprJhUFQKlqKioEadXVYr\",\"sign\":\"" + sign
		+ "\",\"param\":{\"write_date_from\":\"2023-01-01\",\"write_date_to\":\"2023-12-31\"}}}";
	JSONObject jsonObj = JSON.parseObject(jparam);
	System.out.println(jsonObj.toString());
//	JSONObject json = HttpSender.postResponse("http://10.82.8.23:32000" + apiRoute, jsonObj);
//	System.out.println(json.toString());
	return;
    }

}